home *** CD-ROM | disk | FTP | other *** search
- /* file: digester.c
- purpose: digest e-mails from info-mac digest into HTML format
- author: Andre' C. van der Ham
- e-mail: A.C.vanderHam@ET.TUDelft.NL
-
- for THINK C users:
- project profile:
- segment 1: digester
- MacTraps
- segment 2: ANSI
- unix
-
- project type: size: 100K
- flags: 0080 32-bit compatibility
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /*
- #define DEBUG 1
- */
-
- #define MUL "multipart/digest"
- #define BOD "Info-Mac Digest "
- #define EOD "End of Info-Mac Digest"
- #define BOT "Today's Topics:"
- #define EOT "--------------------------------------------------------------------"
- #define EOM "------------------------------\n"
- #define BOF "[Archived as "
- #define FTP "<a href=ftp://sumex-aim.stanford.edu%s>SUMEX</a>"
- #define FUN ", \n<a href=ftp://src.doc.ic.ac.uk/computing/systems/mac%s>UK</a>"
- #define USA ", \n<a href=ftp://ftp.hawaii.edu/mirrors%s>Hawaii</a>"
- #define JAP ", \n<a href=ftp://ftp.u-tokyo.ac.jp/pub%s>U-Tokyo</a>"
- #define CAN ", \n<a href=ftp://ftp.ucs.ubc.ca/pub/mac%s>Canada</a>"
- #define NED ", \n<a href=ftp://ftp.fenk.wau.nl/pub/mac%s>The Netherlands</a>"
- #define SWD ", \n<a href=ftp://ftp.sunet.se/mac%s>Sweden</a>"
- #define MOR 10 /* maximum items overrun */
- #define DRE "http://dutera.et.tudelft.nl/people/vdham/vdham.html"
-
- FILE *outP1, *inP, *outP2, *outP3, *outP;
-
- int ParseOne(int TOCiconOn, int buttonsOn, int split)
- {
- char line[512], tempLine[512], whichFile[512], topic[80];
- int num, vol, issue, i, l;
- int tocItem = 0;
- int msgItem = 0;
- int found, didFtps=0;
- char name[256];
- char *cptr;
-
- for(i=0; i<512; i++) whichFile[i]='m';
-
- /* find issue */
-
- printf("Parsing...\n");
-
- found = 0;
-
- do {
- if( fgets( line, 256, inP ) != NULL )
- {
- if( strstr( line, BOD ) != NULL )
- {
- printf("Found %s\n", BOD);
- printf(line);
- found = 1;
- cptr = (char *)strstr( line, "Volume" );
- if( cptr==NULL )
- {
- printf("Can't find volume number\n");
- return(0);
- }
- vol = atoi( cptr+7 );
- cptr = (char *)strstr( line, "Issue" );
- if( cptr==NULL )
- {
- printf("Can't find issue number\n");
- return(0);
- }
- issue = atoi( cptr+6 );
- if( !split )
- {
- sprintf(name, "IM%d-%d.html", issue, vol);
- printf("name = '%s'\n", name);
- if( (outP1 = fopen( name,"w")) == NULL )
- {
- printf("Can't open '%s'\n", name );
- return(0);
- }
- }
- else /* split files */
- {
- sprintf(name, "IMt%d-%d.html", issue, vol);
- printf("name = '%s'\n", name);
- if( (outP1 = fopen( name,"w")) == NULL )
- {
- printf("Can't open '%s'\n", name );
- return(0);
- }
-
- sprintf(name, "IMp%d-%d.html", issue, vol);
- printf("name = '%s'\n", name);
- if( (outP2 = fopen( name,"w")) == NULL )
- {
- printf("Can't open '%s'\n", name );
- return(0);
- }
-
- sprintf(name, "IMm%d-%d.html", issue, vol);
- printf("name = '%s'\n", name);
- if( (outP3 = fopen( name,"w")) == NULL )
- {
- printf("Can't open '%s'\n", name );
- return(0);
- }
-
- }
- }
- }
- else
- return(0);
- } while( !found );
-
- #ifdef DEBUG
- printf("Title found\n");
- #endif
-
- fprintf(outP1,"<TITLE>Volume %d : Issue %d</TITLE>\n", vol, issue);
- fprintf(outP1,"<pre>\n");
- fprintf(outP1,"<a href=index.html#issue%d-%d><img src=digester.gif alt=Index></a> %s",
- issue, vol, line);
- fprintf(outP1,"<hr>\n\n");
-
- if( split )
- {
- fprintf(outP2,"<TITLE>Volume %d : Issue %d</TITLE>\n", vol, issue);
- fprintf(outP2,"<pre>\n");
- fprintf(outP2,"<a href=index.html#issue%d-%d><img src=digester.gif alt=Index></a> %s",
- issue, vol, line);
- fprintf(outP2,"<hr>\n\n");
-
- fprintf(outP3,"<TITLE>Volume %d : Issue %d</TITLE>\n", vol, issue);
- fprintf(outP3,"<pre>\n");
- fprintf(outP3,"<a href=index.html#issue%d-%d><img src=digester.gif alt=Index></a> %s",
- issue, vol, line);
- fprintf(outP3,"<hr>\n\n");
- }
-
- /* find toc */
-
- found = 0;
-
- do {
- if( fgets( line, 256, inP ) != NULL )
- {
- if( strstr( line, BOT ) != NULL )
- {
- found = 1;
- }
- }
- else
- {
- fclose( outP1 );
- if(split)
- {
- fclose(outP2);
- fclose(outP3);
- }
- return(0);
- }
- } while( !found );
-
-
- #ifdef DEBUG
- printf("TOC found\n");
- #endif
-
- line[strlen(line)-1] = 0;
- fprintf(outP1, "<a name=toc>%s</a>\n", line);
-
- fgets( line, 256, inP ); /* get empty line */
- fputs("<ul>\n", outP1);
-
- while( strlen(fgets( line, 256, inP ))>1 )
- {
- i=0;
- while( i<strlen(line) && line[i]==' ' ) i++;
- if( i==strlen(line) ) i=0;
-
- tocItem++;
- line[strlen(line)-1]=0; /* terminate line with 0 */
-
- if( strstr( (line+i), "[*]" )!=NULL )
- {
- whichFile[tocItem] = 'p';
- strcpy(topic, "prog_topic.gif");
- }
- else
- strcpy(topic, "msg_topic.gif");
-
- if( TOCiconOn )
- {
- if(split)
- {
- fprintf(outP1, "<img src=%s alt=*><a name=toc%d> </a><a href=IM%c%d-%d.html#item%d>%s</a>\n",
- topic, tocItem, whichFile[tocItem], issue, vol, tocItem, line+i);
- }
- else
- {
- fprintf(outP1, "<img src=%s alt=*><a name=toc%d> </a><a href=#item%d>%s</a>\n",
- topic, tocItem, tocItem, line+i);
- }
- }
- else
- {
- if(split)
- {
- fprintf(outP1, "<li><a name=toc%d> </a><a href=IM%c%d-%d.html#item%d>%s</a>\n",
- tocItem, whichFile[tocItem], issue, vol, tocItem, line+i);
- }
- else
- {
- fprintf(outP1, "<li><a name=toc%d> </a><a href=#item%d>%s</a>\n",
- tocItem, tocItem, line+i);
- }
- }
- }
-
- fprintf(outP1, "</ul>\n\n\n");
-
- do
- {
- fgets(line, 256, inP);
- fputs( line, outP1 );
- }
- while( (strstr( line, EOT ) == NULL) && strcmp( line, EOM ) );
-
- /* do messages */
-
- found = 0;
-
- do{
- msgItem++;
-
- if(!split) outP=outP1;
- else if( whichFile[msgItem]=='p' ) outP=outP2;
- else outP=outP3;
-
- if( (whichFile[msgItem]=='p')&&(didFtps==0) )
- {
- fputs("\n</pre>", outP);
- fprintf(outP, "Make FTP connection with: ");
- strcpy( name, "/info-mac/");
-
- fprintf( outP, FTP, name);
- fprintf( outP, FUN, name);
- fprintf( outP, USA, name);
- fprintf( outP, JAP, name);
- fprintf( outP, CAN, name);
- fprintf( outP, NED, name);
- fprintf( outP, SWD, name);
-
- fprintf( outP, ".\n<p><hr><p>\n<pre>\n" );
-
- didFtps=1;
- }
-
- fprintf( outP, "<a name=item%d> </a>",msgItem);
-
- if( buttonsOn )
- {
- if(split && (whichFile[msgItem+1]!=whichFile[msgItem]) )
- fprintf( outP, "<a href=IM%c%d-%d.html#item%d><img src=next.gif alt=Next></a> ", whichFile[msgItem+1], issue, vol, msgItem+1);
- else fprintf( outP, "<a href=#item%d><img src=next.gif alt=Next></a> ", msgItem+1);
-
- if( msgItem>tocItem )
- if(split) fprintf( outP, "<a href=IMt%d-%d.html#toc><img src=toc.gif alt=TOC></a> ", issue, vol);
- else fprintf( outP, "<a href=#toc><img src=toc.gif alt=TOC></a> ");
- else
- if(split) fprintf( outP, "<a href=IMt%d-%d.html#toc%d><img src=toc.gif alt=TOC></a> ", issue, vol, msgItem);
- else fprintf( outP, "<a href=#toc%d><img src=toc.gif alt=TOC></a> ", msgItem);
-
- fprintf( outP, "<a href=index.html#issue%d-%d><img src=digester.gif alt=Index></a>\n\n",
- issue, vol);
- }
- else
- {
- if(split && (whichFile[msgItem+1]!=whichFile[msgItem]) )
- fprintf( outP, "<a href=IM%c%d-%d.html#item%d>Next</a> ", whichFile[msgItem+1], issue, vol, msgItem+1);
- else fprintf( outP, "<a href=#item%d>Next</a> ", msgItem+1);
-
- if( msgItem>tocItem )
- if(split) fprintf( outP, "<a href=IMt%d-%d.html#toc>TOC</a> ", issue, vol);
- else fprintf( outP, "<a href=#toc>TOC</a> ");
- else
- if(split) fprintf( outP, "<a href=IMt%d-%d.html#toc%d>TOC</a> ", issue, vol, msgItem);
- else fprintf( outP, "<a href=#toc%d>TOC</a> ", msgItem);
-
- fprintf( outP, "<a href=index.html#issue%d-%d>INDEX</a>\n\n", issue, vol );
- }
-
- while( strcmp( fgets( line, 256, inP ), EOM )!=0 &&
- strstr( line, EOD ) == NULL && !feof(inP) )
- {
- /* filter entities in mail */
-
- tempLine[0]=0;
- l = 0;
- for(i=0; i<strlen(line); i++ )
- {
- if( line[i]=='<' )
- {
- tempLine[l++] = '&';
- tempLine[l++] = 'l';
- tempLine[l++] = 't';
- tempLine[l++] = ' ';
- }
- else
- if( line[i]=='>' )
- {
- tempLine[l++] = '&';
- tempLine[l++] = 'g';
- tempLine[l++] = 't';
- tempLine[l++] = ' ';
- }
- else
- if( line[i]=='"' )
- {
- tempLine[l++] = '&';
- tempLine[l++] = 'q';
- tempLine[l++] = 'u';
- tempLine[l++] = 'o';
- tempLine[l++] = 't';
- tempLine[l++] = ' ';
- }
- else
- if( line[i]=='&' )
- {
- tempLine[l++] = '&';
- tempLine[l++] = 'a';
- tempLine[l++] = 'm';
- tempLine[l++] = 'p';
- tempLine[l++] = ' ';
- }
- else
- {
- tempLine[l++] = line[i];
- tempLine[l] = 0;
- }
- }
- strcpy( line, tempLine );
-
- fputs(line, outP);
-
- if( (cptr=strstr( line, "/info-mac/" )) != NULL )
- {
- num = cptr-line;
- i = 0;
- while( line[num] != ';' && num<strlen(line) && i<256 )
- {
- name[i]=line[num];
- i++;
- num++;
- }
- name[i] = 0;
- line[strlen(line)-1]=0;
-
- fprintf( outP, "\n</pre>\nFetch from " );
-
- fprintf( outP, FTP, name);
- fprintf( outP, FUN, name);
- fprintf( outP, USA, name);
- fprintf( outP, JAP, name);
- fprintf( outP, CAN, name);
- fprintf( outP, NED, name);
- fprintf( outP, SWD, name);
-
- fprintf( outP, ".\n<pre>\n" );
- }
- }
- fputs(line, outP);
-
- }while( strstr( line, EOD ) == NULL && msgItem<(tocItem+MOR) && !feof( inP ) );
-
- fprintf( outP1, "</pre>\n\n\n" );
- if(!split) fputs("<p><hr><p>\n", outP1);
- fprintf( outP1, "<a name=item%d> </a>",msgItem+1);
- fputs("HTML file created by digester<p>\n", outP1);
- fprintf(outP1,"Digester developed by: <a href=%s>André C. van der Ham</a><p>\n", DRE);
- fputs("<address>\n", outP1);
- fputs("Send bug reports, questions, etc. to: A.C.vanderHam@ET.TUDelft.NL<p>\n", outP1);
- fputs("</address>\n", outP1);
-
- fclose( outP1 );
-
- if(split)
- {
- fprintf( outP2, "</pre>\n\n\n" );
- fprintf( outP2, "<a name=item%d> </a>",msgItem+1);
- fputs("HTML file created by digester<p>\n", outP2);
- fprintf(outP2,"Digester developed by: <a href=%s>André C. van der Ham</a><p>\n", DRE);
- fputs("<address>\n", outP2);
- fputs("Send bug reports, questions, etc. to: A.C.vanderHam@ET.TUDelft.NL<p>\n", outP2);
- fputs("</address>\n", outP2);
-
- fclose( outP2 );
-
- fprintf( outP3, "</pre>\n\n\n" );
- fputs("<p><hr><p>\n", outP3);
- fprintf( outP3, "<a name=item%d> </a>",msgItem+1);
- fputs("HTML file created by digester<p>\n", outP3);
- fprintf(outP3,"Digester developed by: <a href=%s>André C. van der Ham</a><p>\n", DRE);
- fputs("<address>\n", outP3);
- fputs("Send bug reports, questions, etc. to: A.C.vanderHam@ET.TUDelft.NL<p>\n", outP3);
- fputs("</address>\n", outP3);
-
- fclose( outP3 );
- }
-
- printf("file closed.\n");
-
- if( feof( inP ) )
- printf("Premature end of file found on input file.\n");
-
- if( msgItem != tocItem )
- printf("!! msgItem = %d, tocItem = %d\n", msgItem, tocItem );
-
- return(1);
- }
-
- int main( void )
- {
- char name[80], answer[80];
- char line[256];
- int ButtonsOn=0, TOCiconOn=0, split=0;
-
- int okay=1;
-
- puts("\n\n*** D I G E S T E R ***\n");
- puts("by Andre' C. van der Ham\n");
- puts("This program parses info-mac digests you get by e-mail.");
- puts("Save the e-mail without headers and paragraph recognition (Eudora)");
- puts("Convert file to appropriate ascii format, for example: Mac->Unix\n");
- puts("The file can contain a number of digests.");
- puts("An HTML file is generated for each issue.");
- puts("Send bug reports, questions, etc... to:");
- puts("e-mail: A.C.vanderHam@ET.TUDelft.NL\n\n");
-
- printf("Input file (infomac.txt): ");
- gets( name );
- if(strlen(name)<2) strcpy( name, "infomac.txt");
-
- printf("Use fancy topics ICON {gif: topic.gif} (y/N): ");
- gets( answer );
-
- if( answer[0]=='y' ) TOCiconOn=1;
-
- printf("Use fancy buttons {gifs: next.gif and toc.gif} (y/N): ");
- gets( answer );
-
- if( answer[0]=='y' ) ButtonsOn=1;
-
- printf("Split file into 3 separate files for topics, programs and messages (y/N): ");
- gets( answer );
-
- if( answer[0]=='y' ) split=1;
-
- if( (inP = fopen( name, "r" )) != NULL )
- {
- while( okay )
- {
- okay = ParseOne(TOCiconOn, ButtonsOn, split);
-
- #ifdef DEBUG
- printf("%d: next...\n", okay);
- #endif
-
- }
-
- fclose( inP );
- }
-
- return(0);
- }